<?php 
/**
 * 分类管理控制器
 */
class CategoryController extends CommonController{
	/**
	 * 显示列表页
	 */
	 public function index(){
	 	$cid = (int)$_GET['cid'];
		//面包屑导航
		$data = M('hd_category')->all();
		$temp = $this->getFather($data,$cid);
		$temp = array_reverse($temp);
		$this->assign('temp', $temp);
		//分类信息
		$sonData = M('hd_category')->where("pid={$cid}")->all();
		if(empty($sonData)){
			$f = M('hd_category')->where("cid={$cid}")->find();
			$sonData = M('hd_category')->where("pid={$f['pid']}")->all();
		}
		$this->assign('sonData', $sonData);
		
		//不同问题处理
		$w = isset($_GET['w']) ? (int)$_GET['w'] : 0;
		switch ($w) {
			//如果是待解决情况
			case 0:
			//待解决问题
				$sql = "SELECT * FROM hd_ask AS a JOIN hd_category AS c ON a.cid=c.cid WHERE solve=0";
				break;
			case 1:
			//已解决问题
				$sql = "SELECT * FROM hd_ask AS a JOIN hd_category AS c ON a.cid=c.cid WHERE solve=1";
				break;
			case 3:
			//高悬赏
				$sql = "SELECT * FROM hd_ask AS a JOIN hd_category AS c ON a.cid=c.cid WHERE reward>30";
				break;
			case 2:
			//零回答
				$sql = "SELECT * FROM hd_ask AS a JOIN hd_category AS c ON a.cid=c.cid WHERE a.answer=0";
				break;	
		}
		//必须是当前页面的分类下面的问题，必须有cid条件
		$sql .= " and a.cid={$cid} limit 5";
		$askData = M('hd_ask')->query($sql);
//		p($askData);die;
		foreach($askData as $k=>$v){
			$askData[$k]['time'] = date('Y-m-d',$v['time']);
		}
//		p($askData);die;
		$this->assign('askData',$askData);
		
		$this->rightCate();
		$this->topCate();
		$this->display();	
	}
	/**
	 * 显示单个问题页
	 */
	 public function show(){
//	 	问题信息数组
	 	$asid = $_GET['asid'];
		$sql = "select a.asid,a.cid,a.solve,u.username,a.content,a.reward,a.time from hd_ask as a join hd_user as u on a.uid=u.uid where a.asid={$asid}";
		$askinfo = M('hd_ask')->query($sql);
		$askinfo[0]['time'] = date('Y-m-d',$askinfo[0]['time']);
		$this->assign("askinfo", $askinfo[0]);
		//答案信息数组
		$anwinfo = M('hd_answer')->where("asid={$asid} and accept=0")->all();
		foreach($anwinfo as $k=>$v){
			$anwinfo[$k]['time'] = date('Y-m-d',$v['time']);
		}
		$count = M('hd_answer')->where("asid={$asid}")->field('count(*) as c')->find();
//		p($count["c"]);die;
		$this->assign('c', $count["c"]);
		$this->assign("anwinfo", $anwinfo);
		//待解决的相关问题信息
		$notaskinfo = M('hd_ask')->where("cid={$askinfo[0]['cid']} and solve=0")->limit(5)->all();
		foreach($notaskinfo as $k=>$v){
			$notaskinfo[$k]['time'] = date('Y-m-d',$v['time']);
//			p($v['time']);
		}
		
		$this->assign('notaskinfo', $notaskinfo);
		//满意回答信息
		if($askinfo[0]['solve']){
			$satisfy = M('hd_answer')->query("SELECT *,u.accept as a FROM hd_user AS u JOIN hd_answer AS a ON a.uid=u.uid where asid={$asid} and a.accept=1");
	//		$satisfy = M('hd_answer')->where("asid={$asid} and accept=1")->find();
	//		p($satisfy);
			$satisfy[0]['adopt'] = round(($satisfy[0]['a']/$satisfy[0]['answer'])*100);
			$this->assign('satisfy', $satisfy[0]);
		}
		
//		 面包屑导航

		$data = M('hd_category')->all();
		$temp = $this->getFather($data,$askinfo[0]['cid']);
		$temp = array_reverse($temp);
		$this->assign('temp', $temp);
		
		$this->topCate();
		$this->rightCate();
	 	$this->display();
	 }
	/**
	 * 面包屑导航
	 */
	 private function getFather($data,$cid){
	 	$temp = array();
	 	foreach($data as $v){
	 		if($cid == $v['cid']){
	 			$temp[] = $v;
				$temp = array_merge($temp,$this->getFather($data,$v['pid']));
	 		}
	 	}
		return $temp;
	 }
}
